package com.sftz.framework.mvc.actionfilter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.nutz.json.JsonFormat;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.view.ViewWrapper;

import com.sftz.framework.mvc.annotation.NoFilter;
import com.sftz.framework.rersult.LoginResult;
import com.sftz.modules.common.dto.WxMinappUserDto;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.web.chain.support.JsonResult;
import com.uxuexi.core.web.view.Utf8JsonTransferView;

/**
 * 小程序登录拦截器
 * @author 朱晓川
 *
 */
public class MinappLoginFilter implements ActionFilter {

	@Override
	public View match(ActionContext ac) {
		if (ac.getModule().getClass().isAnnotationPresent(NoFilter.class)) {
			return null;
		}
		Utf8JsonTransferView jsonView = new Utf8JsonTransferView(JsonFormat.full());
		HttpServletRequest request = ac.getRequest();
		String token = request.getParameter("token");
		if (Util.isEmpty(token)) {
			return new ViewWrapper(new Utf8JsonTransferView(new JsonFormat()), JsonResult.error("授权token不能为空"));
		}

		WxMinappUserDto user = RedissionUserCache.getUser(token);
		if (Util.isEmpty(user)) {
			Map<String, Object> data = LoginResult.error("对不起,您尚未登录,或登录已失效。", null);
			jsonView.setData(data);
			return jsonView;
			//return new ViewWrapper(new Utf8JsonTransferView(new JsonFormat()), JsonResult.error("对不起，您尚未登录或登录已失效"));
		}
		return null;
	}

}
